package com.css.apps.base.user.action.login;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.Constants;
import com.css.apps.base.common.provider.ComProvider;
import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.spi.IUserListener;
import com.css.common.PluginBus;
import com.css.core.configuration.Environment;
import com.css.core.model.IUser;
import com.css.db.memcached.MemCachedFactory;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.Messages;
import com.css.webbase.WebBaseUtil;
import com.css.webbase.webwork.action.AbstractAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;

@SuppressWarnings("serial")
public class Quit extends AbstractAction {
	private static Log log = LogFactory.getLog(Quit.class);
	private String toUrl = "";
	public String go() {
		try {
			IUser user = WebBaseUtil.getCurrentUser();
			if(user!=null){
				IUserListener  lis=PluginBus.getPlugin(IUserListener.class);
				if(lis!=null)
					lis.beforeLogout((SUser)user);				
				MemCachedFactory.delete(Constants.ACCOUNT_LOGON+user.getUserId());
				//记录日志
				new LogPart()
				.setLogLevel(LogConstant.LOG_LEVEL_COMMON)
				.setOpType(LogConstant.LOG_TYPE_LOGOFF)
				.setResult(LogConstant.RESULT_SUCCESS)
				.setMemo(null)
				.setLogData(Json.object2json(user))
				.setOpObjType(SUser.class.getName())
				.setOpObjId(user.getUserId())
				.setRelObjType(SUser.class.getName())
				.setRelObjId(user.getUserId())
				.setOperId(user.getUserId())
				.setOperName(user.getLoginName())
				.save();
			}
			 
			//ServletActionContext.getRequest().getSession().removeAttribute(Environment.SESSION_LOGIN_KEY);
			ActionContext.getContext().getSession().clear();
			ComProvider cp = new ComProvider();
			cp.clearCookies();
			
			setResult(Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), user!=null?user.getUserId():""));
			return SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}
	public String getToUrl() {
		return toUrl;
	}
	public void setToUrl(String toUrl) {
		this.toUrl = toUrl;
	}
}
